package com.example.lenjor.sprite;
/***
 * author:lenjor
 * createTime:2017.06.29
 * packageName:LoginActivity.class
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

    private EditText et_account;
    private EditText et_password;
    private Button bt_login;
    private Button bt_logout;
    private String account = "";
    private String password = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        BindView();     //绑定控件
        setListener();      //设置监听
    }

    /**
     * function:绑定控件
     */
    private void BindView() {
        et_account = (EditText) findViewById(R.id.account);
        et_password = (EditText) findViewById(R.id.password);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_logout = (Button) findViewById(R.id.bt_logout);
    }

    /**
     * function:设置监听
     */
    private void setListener() {
//        //监听输入账号
//        et_account.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                account = et_account.getText().toString();
//                password = et_password.getText().toString();
//                if(account.length()<6){
//                    bt_login.setBackgroundResource(R.mipmap.btn_login_n);
//                }else {
//                    if(password.length()<6){
//                        bt_login.setBackgroundResource(R.mipmap.btn_login_n);
//                    }else {
//                        bt_login.setBackgroundResource(R.mipmap.btn_login_p);
//                    }
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
//        //监听输入密码
//        et_password.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                account = et_account.getText().toString();
//                password = et_password.getText().toString();
//                if(account.length()<6){
//                    bt_login.setBackgroundResource(R.mipmap.btn_login_n);
//                }else {
//                    if(password.length()<6){
//                        bt_login.setBackgroundResource(R.mipmap.btn_login_n);
//                    }else {
//                        bt_login.setBackgroundResource(R.mipmap.btn_login_p);
//                    }
//                }
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
        //监听退出登陆

        bt_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //监听登陆
        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                account = et_account.getText().toString().trim();
                password = et_password.getText().toString().trim();
                Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
                if (account.length() < 5) {
                    toast.setText("账号至少要求5位");
                    toast.show();
                    return;
                }
                if (password.length() < 3) {
                    toast.setText("密码至少要求3位");
                    toast.show();
                    return;
                }
                if (account.equals("admin") && password.equals("123")) {
                    Intent intent = new Intent();
                    intent.setClass(LoginActivity.this, MainActivity.class);
                    LoginActivity.this.startActivity(intent);
                    finish();
                } else {
                    toast.setText("密码错误");
                    toast.show();
                }
            }
        });
    }
}
